package cc.telecomdigital.tdstock.activity.chart;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import cc.telecomdigital.tdstock.Framework.Activity_Framework;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;

public abstract class LandscapeActivity extends /*BaseActivity*/ Activity_Framework
{

	private SensorManager		sm;

	private boolean			isForward	= false;

	private SensorEventListener	listener	= new SensorEventListener()
									{

										@Override
										public void onAccuracyChanged(
												Sensor sensor,
												int accuracy)
										{

										}

										@Override
										public void onSensorChanged(
												SensorEvent event)
										{
											if (event.sensor
													.getType() == Sensor.TYPE_ACCELEROMETER)
											{

												//				TDStockLog.d("Print_Info", "Sensor.TYPE_ACCELEROMETER");

												float[] values = event.values;

												if (values[1] > 7)
												{

													for (int i = 0; i < values.length; i++)
													{

														TDStockLog
																.d(
																		"Print_Info",
																		"values["
																				+ i
																				+ "]"
																				+ values[i]);
													}

													unRegisterSensorListener();

													if (!isForward)
													{

														isForward = true;
														TDStockLog.d("Print_Info", "called by sensor ");
														forwardToLastActivity();

													}

												}

											}
										}

									};

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		TDStockLog.d("Print_Info", "start onCreate" + "\n");
		sm = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);

	}

	@Override
	protected void onResume()
	{
		super.onResume();
		TDStockLog.d("Print_Info", "start onResume" + "\n");
		unRegisterSensorListener();
		registerSensorListener();
		isForward = false;
	}

	@Override
	protected void onPause()
	{
		super.onPause();
		TDStockLog.d("Print_Info", "start onPause" + "\n");
		unRegisterSensorListener();
	}

	protected void registerSensorListener()
	{
		TDStockLog.d("Print_Info", "register");
		Sensor sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

		if (sensor == null)
		{
			TDStockLog.e("Print_Info", "sensor == null");
			return;
		}

		sm.registerListener(listener, sensor,
				SensorManager.SENSOR_DELAY_NORMAL);

	}

	protected void unRegisterSensorListener()
	{
		TDStockLog.d("Print_Info", "unregister");
		sm.unregisterListener(listener);
	}

	protected abstract void forwardToLastActivity();

}